<?php
   class ShortMessageAction extends CommonAction{
   	    public function _filter(&$map){
   	    	$map['receivername'] = $_SESSION['loginUserName'];
   	    	$map['deleted']=0;
   	    	$map['content'] = array('like',"%".$_POST['loginUserName']."%");
   	    }
	   	public function _before_index() {
	   		$model = M ( "ShortMessage" );
	   		$order='sendtime';
	   		$sort='desc';
	   		$_REQUEST['_order']=$order;
	   		$_REQUEST['_sort']=$sort;
	   	}
	   	public function _before_add() {	   		
	   		$model = M ( "ShortMessage" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   	}	   	
	   	function insert() {
	   		//B('FilterString');
	   		$name=$this->getActionName();
	   		$model = D ($name);
	   		if (false === $model->create ()) {
	   			$this->error ( $model->getError () );
	   		}
	   		$info=$this->getType($name);
	   		$Names=array();
	   		$receivernames=$_REQUEST['receivername'];
	   		if(strpos($receivernames, ',')>0){
	   			$Names=explode(',',$receivernames);
	   		}else{
	   			$Names[0]=$receivernames;
	   		}
	   		for($i=0;$i<count($Names);$i++){
	   			$data[$i]['receivername']=$Names[$i];
	   			$data[$i]['sendername'] = $_REQUEST['sendername'];
	   			$data[$i]['sendtime'] = $_REQUEST['sendtime'];
	   			$data[$i]['content'] = $_REQUEST['content'];
	   			$data[$i]['state'] = $_REQUEST['state'];
	   			$data[$i]['msgtype']=$_REQUEST['msgtype'];
	   		}	   	
	   		//保存当前数据对象
	   		$list=$model->addAll ($data);
	   		if ($list!==false) { //保存成功
	   			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
	   			$this->insertLog('添加','操作'.$info.'编号为'.$list.'的数据成功');
	   			$this->ajaxReturn (1,'发送成功!',200);
	   		} else {
	   			//失败提示
	   			$this->insertLog('添加','操作'.$info.'编号为'.$list.'的数据失败');
	   			$this->ajaxReturn (1,'发送失败!',300);
	   		}
	   	}
	   	public function _before_edit() {
	   		$model = M ( "ShortMessage" );
	   		$list = $model->select ();
	   		$this->assign ( 'list', $list );
	   	}
	   	public function modifyState(){
	   		$id=$_REQUEST['id'];
	   		$model = M('ShortMessage');
	   		$state=$model->where('id='.$id)->getField('state');
	   		if($state==1){
	   			$this->ajaxReturn(1,'不能标记已读信息！',300);
	   		}
	   		$result=$model->where('id='.$id)->setField('state','1');
	   		if($result!==false){
	   			$this->ajaxReturn (1,'标记为已读!',200);
	   		}else{
	   			$this->ajaxReturn (1,'标记失败!',300);
	   		}
	   	}
	   	public function readmsg($id=0){	   		
	   		$model = M('ShortMessage');	   		
	   		$this->vo = $model->find($id);
	   		$name=$model->where('id='.$id)->getField('sendername');
	   		$_SESSION['replyname']=$name;
	   		$model->where('id='.$id)->setField('state','1');
	   		$data['readtime']=date('Y-n-j H:i:s');	
	   		$result=$model->where('id='.$id)->save($data);
	   		$this->display();
	   	}
	   	public function chooseReceiver(){
	   		$User = M('User');
	   		$list=$User->select();
	   		$this->assign('list',$list);
	   		$this->display();
	   	}
	   	public function reply(){
	   		$this->display();
	   	}
	   	public function outbox(){
	   		$model=M('ShortMessage');
	   		$sendername=$_SESSION['loginUserName'];
	   		import ( '@.Org.Page' );
	   		$count = $model->where("sendername='".$sendername."'")->count ();
	   		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
	   		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
	   		$list = $model->where("sendername='".$sendername."'")->page ( $pageNum . "," . $page->listRows )->order('sendtime desc')->select ();	   		
	   		$this->assign('list',$list);
	   		$this->assign ( 'totalCount', $count ); // 总条数
	   		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
	   		$this->assign ( 'currentPage', $pageNum ); // 当前页
	   		$this->display();
	   	}
	   	public function receive(){
	   		$model=M('ShortMessage');
	   		$receivername=$_SESSION['loginUserName'];
	   		import ( '@.Org.Page' );
	   		$count = $model->where("receivername='".$receivername."' and deleted=0")->count ();
	   		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
	   		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
	   		$list = $model->where("receivername='".$receivername."' and deleted=0")->page ( $pageNum . "," . $page->listRows )->order('sendtime desc')->select ();	   		
	   		$this->assign('list',$list);
	   		$this->assign ( 'totalCount', $count ); // 总条数
	   		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
	   		$this->assign ( 'currentPage', $pageNum ); // 当前页
	   		$this->display();
	   	}
	   	public function checkreceipt($id=0){
	   		$model = M('ShortMessage');
	   		$this->vo = $model->find($id);
	   		$this->display();
	   	}
	   	public function foreverdelete() {
	   		//删除指定记录
	   		$name=$this->getActionName();
	   		$info=$this->getType($name);
	   		$model = D ($name);
	   		if (! empty ( $model )) {
	   			$pk = $model->getPk ();
	   			$id = $_REQUEST ['ids'];
	   			if (isset ( $id )) {
	   				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
	   				$data['deleted']=1;
	   				$result=$model->where($condition)->save($data);
	   				if (false !== $result) {	   					
	   					$this->insertLog('删除','操作'.$info.'编号为'.$id.'的数据成功');
	   					$this->ajaxReturn (1,'删除成功!',200);
	   				} else {
	   					$this->insertLog('删除','操作'.$info.'编号为'.$id.'的数据失败');
	   					$this->ajaxReturn (1,'删除失败!',300);
	   				}
	   			} else {
	   				$this->ajaxReturn (1,'进行删除操作出现非法操作!',300);
	   			}
	   		}
	   	}
   }
?>